import 'dart:async';
import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lejiaxueche/utils/methods.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../routes/routes.dart';

class StartPage extends StatefulWidget {
  @override
  _StartPageState createState() => _StartPageState();
}

class _StartPageState extends State<StartPage> {
  Timer _timer;
  int count = 5;

  @override
  void initState() {
    getSubjectFromStorage();
    super.initState();
  }

  @override
  void dispose() {
    if(_timer != null) {
      _timer.cancel();
      _timer = null;
    }
    super.dispose();
  }

  void getSubjectFromStorage() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String subject1 = prefs.getString('subject1');
    Duration time = new Duration(seconds: 1);
    _timer = Timer.periodic(time, (timer) {
      if(this.count <= 0) {
        timer.cancel();
        timer = null;
        if(subject1 != null) {
          Routes.router.navigateTo(
            context,
            '/navs',
            transition: TransitionType.inFromRight,
            clearStack: true
          );
        }else{
          Routes.router.navigateTo(
            context,
            '/start/subject',
            clearStack: true,
            transition: TransitionType.inFromRight
          );
        }
      }else{
        setState(() {
          count = this.count - 1;
        });
      }
    });
  }

  void navToPage() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String subject1 = prefs.getString('subject1');
    if(subject1 != null) {
      Routes.router.navigateTo(
        context,
        '/navs',
        transition: TransitionType.inFromRight,
        clearStack: true
      );
    }else{
      Routes.router.navigateTo(
        context,
        '/start/subject',
        clearStack: true,
        transition: TransitionType.inFromRight
      );
    }
  }

  Widget countDown() {
    return GestureDetector(
      onTap: () {
        if(_timer != null) {
          _timer.cancel();
          _timer = null;
        }
        navToPage();
      },
      child: Container(
        width: fitSize(160),
        height: fitSize(60),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Colors.black12,
          borderRadius: BorderRadius.all(Radius.circular(fitSize(30)))
        ),
        child: Text(
          '跳过(${this.count}s)',
          style: TextStyle(
            color: Colors.white70,
            fontSize: fitSize(26),
            decoration: TextDecoration.none,
            fontWeight: FontWeight.w400
          )
        ),
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Stack(
      children: <Widget>[
        Container(
          width: size.width,
          height: size.height,
          alignment: Alignment.topCenter,
          color: Color(0xff00a3ea),
          child: Image.asset(
            'assets/mock/start.jpg',
            fit: BoxFit.fitWidth
          ),
        ),
        Positioned(
          right: fitSize(40),
          bottom: fitSize(100),
          child: countDown(), 
        )
      ],
    );
  }
}